﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Web;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using HtmlAgilityPack;

namespace EntLib.Domain.Common
{
    public class CommHelper
    {
        public static string GetHtmlDocument(string url)
        {
            HtmlWeb web = new HtmlWeb();
            HtmlDocument document = web.Load(url);
            //删除脚本
            foreach (var script in document.DocumentNode.Descendants("script").ToArray())
                script.Remove();
            //删除样式
            foreach (var style in document.DocumentNode.Descendants("style").ToArray())
                style.Remove();
            //删除注释
            foreach (var comment in document.DocumentNode.SelectNodes("//comment()").ToArray())
                comment.Remove();
            return document.DocumentNode.InnerHtml;
        }

        /// <summary>
        /// 判断 DataReader 里面是否包含指定的列
        /// </summary>
        /// <param name="idr"></param>
        /// <param name="columnName"></param>
        /// <returns></returns>
        public static bool ReaderExists(IDataReader idr, string columnName)
        {
            int count = idr.FieldCount;
            for (int i = 0; i < count; i++)
            {
                if (idr.GetName(i).Equals(columnName))
                {
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 从IDataReader,获取数据列集合
        /// </summary>
        /// <param name="idr">IDataReader</param>
        /// <returns></returns>
        public static IList<string> GetFieldsFromIDataReader(IDataReader idr)
        {
            IList<string> fields = new List<string>();
            int count = idr.FieldCount;
            for (int i = 0; i < count; i++)
            {
                fields.Add(idr.GetName(i));
            }
            return fields;
        }
        /// <summary>
        /// 将对象保存到Cookie中
        /// </summary>
        /// <param name="obj">对象</param>
        /// <param name="cookieName">cookie名称</param>
        public static void SaveObjectToCookies(object obj, string cookieName)
        {
            BinaryFormatter bf = new BinaryFormatter();  //声明一个序列化类
            MemoryStream ms = new MemoryStream();  //声明一个内存流
            bf.Serialize(ms, obj);  //执行序列化操作
            byte[] result = new byte[ms.Length];
            result = ms.ToArray();
            string temp = System.Convert.ToBase64String(result);    /*此处为关键步骤，将得到的字节数组按照一定的编码格式转换为字符串，不然当对象包含中文时，进行反序列化操作时会产生编码错误*/
            ms.Flush();
            ms.Close();
            HttpCookie cookie = new HttpCookie(cookieName);  //声明一个Key为person的Cookie对象
            cookie.Expires = DateTime.Now.AddDays(1.0);  //设置Cookie的有效期到明天为止,此处时间可以根据需要设置
            cookie.Value = temp;  //将cookie的Value值设置为temp
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
        /// <summary>
        /// 从Cookie中取出保存的对象值
        /// 使用方法：如，Person person = GetObjectFromCookies("person") as Person;
        /// </summary>
        /// <param name="cookieName">cookie名称</param>
        /// <returns></returns>
        public static object GetObjectFromCookies(string cookieName)
        {
            string result = HttpContext.Current.Request.Cookies[cookieName].Value;
            byte[] b = System.Convert.FromBase64String(result);  //将得到的字符串根据相同的编码格式分成字节数组
            MemoryStream ms = new MemoryStream(b, 0, b.Length);  //从字节数组中得到内存流
            BinaryFormatter bf = new BinaryFormatter();
            //Person person = bf.Deserialize(ms) as Person;  //反序列化得到Person类对象
            return bf.Deserialize(ms);
        }
    }
}
